import javax.swing.*;

//import ScenarioGenerator.DistributionType;

import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;

@SuppressWarnings("serial")
public class ScenarioGeneratorGUI extends JFrame  implements ActionListener
{
	private ScenarioGenerator generator; 
	
	private JLabel initialBudgetLabel;
	private JTextField initialBudget;
	
	private JLabel stepBudgetLabel;
	private JTextField stepBudget;
	
	private JLabel productTypeCountLabel;
	private JTextField productTypeCount;

	private JLabel minProductQualityLabel;
	private JLabel maxProductQualityLabel;
	private JTextField minProductQuality;
	private JTextField maxProductQuality;
	
	private JLabel qualityDistribution;
	private JRadioButton uniformQuality;
	private JRadioButton gaussianQuality;
	
	private JLabel minProductPriceLabel;
	private JLabel maxProductPriceLabel;
	private JTextField minProductPrice;
	private JTextField maxProductPrice;
	
	private JLabel priceDistribution;
	private JRadioButton uniformPrice;
	private JRadioButton gaussianPrice;
	
	private JLabel minAbilitiesLabel;
	private JLabel maxAbilitiesLabel;
	private JTextField minAbilities;
	private JTextField maxAbilities;
	
	private JLabel minBuyCountLabel;
	private JLabel maxBuyCountLabel;
	private JTextField minBuyCount;
	private JTextField maxBuyCount; 
	
	private JButton generate;
	
	private Object lock;
	
	public ScenarioGeneratorGUI(ScenarioGenerator generator, Object lock)
	{
		super("Trust and Reputation - Scenario Generator");
		
		this.generator = generator;
		this.lock = lock;
		
		getContentPane().setLayout(new GridLayout(14,1));
		
		initialBudgetLabel = new JLabel("Trader initial budget:");
		initialBudget = new JTextField(5);
		JPanel p1 = new JPanel(new GridLayout(1,2));
		p1.add(initialBudgetLabel);
		p1.add(initialBudget);
		initialBudget.setHorizontalAlignment(JTextField.CENTER);
		
		stepBudgetLabel = new JLabel("Trader step budget:");
		stepBudget = new JTextField(5);
		JPanel p2 = new JPanel(new GridLayout(1,2));
		p2.add(stepBudgetLabel);
		p2.add(stepBudget);
		stepBudget.setHorizontalAlignment(JTextField.CENTER);
		
		productTypeCountLabel = new JLabel("Number of product types:");
		productTypeCount = new JTextField(5);
		JPanel p3 = new JPanel(new GridLayout(1,2));
		p3.add(productTypeCountLabel);
		p3.add(productTypeCount);
		productTypeCount.setHorizontalAlignment(JTextField.CENTER);
		
		minProductQualityLabel = new JLabel("Minimum product quality:");
		minProductQuality = new JTextField(5);
		maxProductQualityLabel = new JLabel("Maximum product quality:");
		maxProductQuality = new JTextField(5);
		JPanel p4 = new JPanel(new GridLayout(1,2));
		p4.add(minProductQualityLabel);
		p4.add(minProductQuality);
		minProductQuality.setHorizontalAlignment(JTextField.CENTER);
		JPanel p5 = new JPanel(new GridLayout(1,2));
		p5.add(maxProductQualityLabel);
		p5.add(maxProductQuality);
		maxProductQuality.setHorizontalAlignment(JTextField.CENTER);
		
		qualityDistribution = new JLabel("Product quality distribution:");
		uniformQuality = new JRadioButton("UNIFORM");
		gaussianQuality = new JRadioButton("GAUSSIAN");
		uniformQuality.setSelected(true);
		ButtonGroup bgQ = new ButtonGroup();
		bgQ.add(uniformQuality);
		bgQ.add(gaussianQuality);
		JPanel p6 = new JPanel(new GridLayout(1,3));
		p6.add(qualityDistribution);
		p6.add(uniformQuality);
		p6.add(gaussianQuality);
		
		minProductPriceLabel = new JLabel("Minimum product price:");
		maxProductPriceLabel = new JLabel("Maximum product price:");
		minProductPrice = new JTextField(5);
		maxProductPrice = new JTextField(5);
		JPanel p7 = new JPanel(new GridLayout(1,2));
		p7.add(minProductPriceLabel);
		p7.add(minProductPrice);
		minProductPrice.setHorizontalAlignment(JTextField.CENTER);
		JPanel p8 = new JPanel(new GridLayout(1,2));
		p8.add(maxProductPriceLabel);
		p8.add(maxProductPrice);
		maxProductPrice.setHorizontalAlignment(JTextField.CENTER);
		
		priceDistribution = new JLabel("Product price distribution:");
		uniformPrice = new JRadioButton("UNIFORM");
		gaussianPrice = new JRadioButton("GAUSSIAN");
		ButtonGroup bgP = new ButtonGroup();
		bgP.add(uniformPrice);
		bgP.add(gaussianPrice);
		uniformPrice.setSelected(true);
		JPanel p9 = new JPanel(new GridLayout(1,3));
		p9.add(priceDistribution);
		p9.add(uniformPrice);
		p9.add(gaussianPrice);
		
		minAbilitiesLabel = new JLabel("Minimum trader abilities:");
		maxAbilitiesLabel = new JLabel("Maximum trader abilities:");
		minAbilities = new JTextField(5);
		maxAbilities = new JTextField(5);
		JPanel p10 = new JPanel(new GridLayout(1,2));
		p10.add(minAbilitiesLabel);
		p10.add(minAbilities);
		minAbilities.setHorizontalAlignment(JTextField.CENTER);
		JPanel p11 = new JPanel(new GridLayout(1,2));
		p11.add(maxAbilitiesLabel);
		p11.add(maxAbilities);
		maxAbilities.setHorizontalAlignment(JTextField.CENTER);
		
		minBuyCountLabel = new JLabel("Minimum trader buying habits:");
		maxBuyCountLabel = new JLabel("Maximum trader buying habits:");
		minBuyCount = new JTextField(5);
		maxBuyCount = new JTextField(5);
		JPanel p12 = new JPanel(new GridLayout(1,2));
		p12.add(minBuyCountLabel);
		p12.add(minBuyCount);
		minBuyCount.setHorizontalAlignment(JTextField.CENTER);
		JPanel p13 = new JPanel(new GridLayout(1,2));
		p13.add(maxBuyCountLabel);
		p13.add(maxBuyCount);
		maxBuyCount.setHorizontalAlignment(JTextField.CENTER);
		
		generate = new JButton("Generate");
		generate.addActionListener(this);
		JPanel p14 = new JPanel(new FlowLayout());
		p14.add(generate);
		
		getContentPane().add(p1);
		getContentPane().add(p2);
		getContentPane().add(p3);
		getContentPane().add(p4);
		getContentPane().add(p5);
		getContentPane().add(p6);
		getContentPane().add(p7);
		getContentPane().add(p8);
		getContentPane().add(p9);
		getContentPane().add(p10);
		getContentPane().add(p11);
		getContentPane().add(p12);
		getContentPane().add(p13);
		getContentPane().add(p14);
		
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		pack();
		setVisible(true);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		try
		{
			int iBudget = Integer.parseInt(initialBudget.getText().trim());
			int sBudget = Integer.parseInt(stepBudget.getText().trim());
			int typeCount = Integer.parseInt(productTypeCount.getText().trim());
			int minQ = Integer.parseInt(minProductQuality.getText().trim());
			int maxQ = Integer.parseInt(maxProductQuality.getText().trim());
			ScenarioGenerator.DistributionType distrQ;
			if(uniformQuality.isSelected())
				distrQ = ScenarioGenerator.DistributionType.UNIFORM;
			else
				distrQ = ScenarioGenerator.DistributionType.GAUSSIAN;
			int minP = Integer.parseInt(minProductPrice.getText().trim());
			int maxP = Integer.parseInt(maxProductPrice.getText().trim());
			ScenarioGenerator.DistributionType distrP;
			if(uniformPrice.isSelected())
				distrP = ScenarioGenerator.DistributionType.UNIFORM;
			else
				distrP = ScenarioGenerator.DistributionType.GAUSSIAN;
			int minA = Integer.parseInt(minAbilities.getText().trim());
			int maxA = Integer.parseInt(maxAbilities.getText().trim());
			int minB = Integer.parseInt(minBuyCount.getText().trim());
			int maxB = Integer.parseInt(maxBuyCount.getText().trim());
			
			if(minA > typeCount || minB > typeCount || maxA < minA || maxB < minB || maxQ < minQ || maxP < minP)
				throw new Exception("Incorrect values assigned");
			
			generator.setInitialTraderBudget(iBudget);
			generator.setStepTraderBudget(sBudget);
			generator.setProductTypeCount(typeCount);
			generator.setMinProductQuality(minQ);
			generator.setMaxProductQuality(maxQ);
			generator.setQualityDistribution(distrQ);
			generator.setMinProductPrice(minP);
			generator.setMaxProductPrice(maxP);
			generator.setPriceDistribution(distrP);
			generator.setMinAgentAbilities(minA);
			generator.setMaxAgentAbilities(maxA);
			generator.setMinBuyingHabits(minB);
			generator.setMaxBuyingHabits(maxB);
			
			synchronized (lock) {
				lock.notify();
			}
		}
		catch(Exception err)
		{
			err.printStackTrace();
			JOptionPane.showMessageDialog(this, "Invalid values. "+err.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
	
}
